TtlCache

Ecng.Net

Keeps the answers to a remote question for as long as they are worth keeping.

Constructors

TtlCache
public TtlCache(TimeSpan ttl, TimeSpan maxAge, TimeProvider time)
ttlCache = TtlCache(ttl, maxAge, time)

Initializes a new instance of the TtlCache.

ttl
How long an answer is kept after it was last asked for.
maxAge
The oldest an answer may be, however often it is asked for.
time
The clock an answer's age is measured on.
TtlCache
public TtlCache(TimeSpan ttl, TimeSpan maxAge, TimeProvider time, IEqualityComparer<TKey> comparer)
ttlCache = TtlCache(ttl, maxAge, time, comparer)

Initializes a new instance of the TtlCache.

ttl
How long an answer is kept after it was last asked for.
maxAge
The oldest an answer may be, however often it is asked for.
time
The clock an answer's age is measured on.
comparer
How keys are compared.

Properties

Count
public int Count { get; }
value = ttlCache.Count

Gets the number of answers that have not gone stale.

HeldCount
public int HeldCount { get; }
value = ttlCache.HeldCount

Gets the number of answers held, stale ones included.

Values
public IEnumerable<TValue> Values { get; }
value = ttlCache.Values

Gets the answers held that have not gone stale.

Methods

Clear
public void Clear()
ttlCache.Clear()

Drops every answer held.

GetAsync
public ValueTask<TValue> GetAsync(TKey key, Func<TKey, CancellationToken, ValueTask<TValue>> resolve, CancellationToken cancellationToken)
result = ttlCache.GetAsync(key, resolve, cancellationToken)

Serves the answer for a key, asking when there is none to serve.

key
What the answer is looked up by.
resolve
Where the answer comes from.
cancellationToken
Cancellation token.

Returns: The answer.

GetAsync
public ValueTask<TValue> GetAsync(TKey key, Func<TKey, CancellationToken, ValueTask<TValue>> resolve, TimeSpan ttl, TimeSpan maxAge, CancellationToken cancellationToken)
result = ttlCache.GetAsync(key, resolve, ttl, maxAge, cancellationToken)

Serves the answer for a key, asking when there is none to serve, and holds what it answered for a lifetime of this call's choosing.

key
What the answer is looked up by.
resolve
Where the answer comes from.
ttl
How long this answer is served before the source is asked again.
cancellationToken
Cancellation token.

Returns: The answer.

Remove
public bool Remove(TKey key)
result = ttlCache.Remove(key)

Drops the answer held for a key, for a caller that knows it has changed.

key
What the answer is looked up by.

Returns: true if an answer was held; otherwise, false.

RemoveWhere
public int RemoveWhere(Func<TKey, bool> match)
result = ttlCache.RemoveWhere(match)

Drops every answer whose key the caller names.

match
What the keys to drop have in common.

Returns: How many were dropped.

RemoveWhere
public int RemoveWhere(Func<TKey, TValue, bool> match)
result = ttlCache.RemoveWhere(match)

Drops every answer the caller recognises by what was cached.

match
What the answers to drop have in common.

Returns: How many were dropped.

Set
public void Set(TKey key, TValue value)
ttlCache.Set(key, value)

Holds an answer for a key, for the lifetime this cache was made with.

key
What the answer is looked up by.
value
The answer.
Set
public void Set(TKey key, TValue value, TimeSpan ttl, TimeSpan maxAge)
ttlCache.Set(key, value, ttl, maxAge)

Holds an answer for a key, for a lifetime of this call's choosing.

key
What the answer is looked up by.
value
The answer.
ttl
How long it is kept after it was last asked for.
maxAge
The oldest it may be, however often it is asked for.
TryGet
public bool TryGet(TKey key, TValue value)
result = ttlCache.TryGet(key, value)

Serves the answer held for a key, for a caller that puts them there itself.

key
What the answer is looked up by.
value
The answer.

Returns: true if one is held and has not gone stale; otherwise, false.